﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NoMiddleman.Core.DataInterfaces;
using NoMiddleman.Core.Domain;
using NoMiddleman.Data;
using NoMiddleman.Facades.DataTransferObjects;

namespace NoMiddleman.Facades.Facades
{
    public class ClientFacade
    {
        private readonly IClientRepository _clientRepository;

        public ClientFacade(IClientRepository clientRepository)
        {
            _clientRepository = clientRepository;
        }

        public ICollection<ClientDTO> GetClients()
        {
            ICollection<ClientDTO> coll = new List<ClientDTO>();
            foreach (var client in _clientRepository.GetAll())
            {
                coll.Add(ClientFactory.GetClientDTO(client));
            }
            return coll;
        }
        
        public ClientDTO GetClient(string login)
        {
            Dictionary<string, object> dict = new Dictionary<string, object> { { "Login", login } };
            return ClientFactory.GetClientDTO(_clientRepository.GetByProperties(dict).First());
        }

        public ClientDTO GetClient(int id)
        {
            return ClientFactory.GetClientDTO(_clientRepository.Get(id));
        }

        public ClientDTO CreateClient(ClientDTO client)
        {
            Client clientEntity = ClientFactory.GetClient(client);
            clientEntity.Address = AddressFactory.GetAddress(client.Address);
            return ClientFactory.GetClientDTO(_clientRepository.Save(clientEntity));
        }
    }
}